<?php

namespace Admin\Controller;

class ParentController extends AdminController {
    
    /*
     * 家长列表
     */
    public function parentlist(){

        $where = "  user.type = 2 ";
        $url = '';

        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }

        //姓名搜索
        /*if(I('get.name')){
            $filter['name'] = (int)I('get.name');
            $url .= '&name=' . $filter['name'];
            $where .= " AND ut.relname  LIKE '%" . $filter['relname']."%'";
        }else{
            $filter['name'] = '';
        }

        //电话搜索
        if(I('get.phone')){
            $filter['phone'] = I('get.phone');
            $url .= '&phone=' . $filter['phone'];
            $where .= " AND user.phone=" . $filter['phone'];
        }else{
            $filter['phone'] = '';
        }*/


        $limit = 8;
        $start = ($page - 1) * $limit;

        $model = M("user");
        $data = $model->join('LEFT JOIN user_parent up ON up.user_id=user.id')
            ->where($where)->order('user.status,user.addtime desc')->limit($start,$limit)
            ->field('user.addtime, user.phone, user.id as new_user_id,user.status, user.is_certified,up.cardnum,up.address,up.name')->select();
        $total = $model->where($where)->count();
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Parent/parentList') . '?page={page}' . $url;

        $render = $pagination->render();
        $this->assign('nav','parent');
        $this->assign('data',$data);
        $this->assign('render',$render);
        $this->assign('filter',$filter);
        $this -> display('list');
    }

    /*
     * 家长审核及认证
     * type:1认证， 2：审核
     */
    public function verify(){
        $result = array(
            'flag' => '0',
            'content' => '操作失败'
        );
        if(IS_POST){
            $user = M('user');
            if(I('post.type') == 1 && I('post.user_id')){
                $data['id'] = I('post.user_id');
                $data['is_certified'] = I('post.is_certified');
                $flag = $user->save($data);
                if($flag){
                    $result = array(
                        'flag' => '1',
                        'content' => '操作成功'
                    );
                }
            }elseif(I('post.type') == 2 && I('post.user_id')){
                $data['id'] = I('post.user_id');
                $data['status'] = I('post.status');
                $flag = $user->save($data);
                if($flag){
                    $result = array(
                        'flag' => '1',
                        'content' => '操作成功'
                    );
                }
            }
        }
        $this->ajaxReturn($result);

    }

    public function changeinfo(){
        $user_id = I("post.user_id");
        $name = I("post.name");
        $cardnum = I("post.cardnum");
        $address = I("post.address");
        $user_parent = M("user_parent");
        $map['user_id'] = $user_id;
        $res = $user_parent->where($map)->count();
        if($res==0){
            $data['user_id'] = $user_id;
            $data['name'] = $name;
            $data['cardnum'] = $cardnum;
            $data['address'] = $address;
            $rs1 = $user_parent->add($data);
            if($rs1){
                $data['error'] = 0;
                $data['msg'] = "家长信息修改成功";
                echo json_encode($data);die();
            }else{
                $data['error'] = 1;
                $data['msg'] = "添加家长信息失败";
                echo json_encode($data);die();
            }
        }else{
            $data['name'] = $name;
            $data['cardnum'] = $cardnum;
            $data['address'] = $address;
            $rs1 = $user_parent->where($map)->save($data);
            if($rs1){
                $data['error'] = 0;
                $data['msg'] = "家长信息修改成功";
                echo json_encode($data);die();
            }else{
                $data['error'] = 1;
                $data['msg'] = "修改家长信息失败";
                echo json_encode($data);die();
            }
        }
    }
}

